package com.mxprojects.gui
{
	import com.mxprojects.constructors.DrawBorder;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.Utils;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.ProfileWindow;
	
	import flash.display.MovieClip;
	import flash.events.TextEvent;
	
	public class OnlineUsers extends BaseForumElement
	{
		private var onlineString:String;
		private var userText:SimpleTextField;
		
		//this will just output a simple block of users an links of people online
		
		public function OnlineUsers(onlineUsers:String, recordUsers:int, recordTime:String) {
			//we get a comma seperated string from the server in the format userID|username,userID|username
			var users:Array = onlineUsers.split(",");
			var gap:String = Utils.buildBlankString(Config.ONLINE_USER_GAP_LENGTH);
			
			//start the string
			onlineString = "<span class=\"onlineUsersCount\">";
			
			if(users[0] != ""){		
				if(users.length == 1){
					onlineString += users.length + " " + Language.main(Word.USER_ONLINE) + ": </span>";
				}else{
					onlineString += users.length + " " + Language.main(Word.USERS_ONLINE) + ": </span>";
				}
				
				for(var i:int = 0; i < users.length; i++){
					var userBits:Array = users[i].split("|");
					onlineString += "<a class=\"onlineUsersLink\" href=\"event:"+userBits[1] + "," + userBits[0]+"\">" + userBits[1] +"</a>" + gap;
				}
				
				//trim off the fat
				onlineString = onlineString.substr(0, onlineString.length - gap.length);
			}else{
				onlineString += "0 " + Language.main(Word.USERS_ONLINE) + "</span>";
			}
			
			//add in the record
			onlineString += "\n\n" + "<span class=\"onlineUsersRecord\">" + Language.main(Word.RECORD_USERS_ONLINE) + " (" + recordUsers + "): " + recordTime + "</span>";
			
			//populate the online users text
			userText = new SimpleTextField(onlineString);
			userText.x = Config.TEXT_PADDING;
			userText.y = Config.TEXT_PADDING;
			userText.width = MXP3.wStage - Config.TEXT_PADDING*2;
			userText.wordWrap = true;
			
			//listen for clicks
			userText.addEventListener(TextEvent.LINK, getProfile, false, 0, true);
			
			//draw a box to put the names in
			createBox();
			
			//add the user text after the bg is drawn
			addChild(userText);
			
		}
		
		override public function resize():void {
			background.height = userText.height + Config.TEXT_PADDING*2
			userText.width = MXP3.wStage - Config.TEXT_PADDING*2;
			
			super.resize();
		}
		
		private function createBox():void {
			//this will draw the needed UI
			background = new MovieClip();
			background.graphics.beginFill(0x000000);
			background.graphics.drawRect(0, 0, MXP3.wStage, userText.height + Config.TEXT_PADDING*2);
			background.graphics.endFill();
						
			addChild(background);
			
			//set the value based on CSS
			super.init(".onlineUsersBackground");
		}
		
		private function getProfile(event:TextEvent):void {
			//split the link and open the profile window
			var chunks:Array = event.text.split(",");
			ProfileWindow.view(chunks[0], chunks[1]);
		}

	}
}